package labox.innovation.gameserver.model.actor.instance;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import labox.innovation.gameserver.ai.CtrlIntention;
import labox.innovation.gameserver.model.L2CharPosition;
import labox.innovation.gameserver.model.actor.L2Attackable;
import labox.innovation.gameserver.model.actor.L2Character;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance.KarmaStat;
import labox.innovation.gameserver.model.actor.knownlist.GuardKnownList;
import labox.innovation.gameserver.model.base.Race;
import labox.innovation.gameserver.model.quest.Quest;
import labox.innovation.gameserver.templates.chars.NpcTemplate;

/**
 * 世界中的守卫逻辑.
 * <p>
 * It inherits all methods from L2Attackable and adds some more such as tracking PK and aggressive L2MonsterInstance.<BR>
 * <BR>
 * 
 * @version $Revision: 1.11.2.1.2.7 $ $Date: 2005/04/06 16:13:40 $
 */
public final class FuzeGuardInstance extends L2Attackable {

	private static Logger _log = LoggerFactory.getLogger(FuzeGuardInstance.class.getName());

	/**
	 * Constructor of L2GuardInstance (use L2Character and L2NpcInstance constructor).<BR>
	 * <BR>
	 * 
	 * <B><U> Actions</U> :</B><BR>
	 * <BR>
	 * <li>Call the L2Character constructor to set the _template of the L2GuardInstance (copy skills from template to object and link _calculators to NPC_STD_CALCULATOR)</li> <li>Set the name of the L2GuardInstance</li> <li>Create a RandomAnimation Task that will be launched after the calculated delay if the server allow it</li><BR>
	 * <BR>
	 * 
	 * @param objectId
	 *            Identifier of the object to initialized
	 * @param NpcTemplate
	 *            Template to apply to the NPC
	 */
	public FuzeGuardInstance(long objectId, NpcTemplate template) {
		super(objectId, template);
	}

	@Override
	public final GuardKnownList getKnownList() {
		return (GuardKnownList) _knownList;
	}

	@Override
	public void initKnownList() {
		setKnownList(new GuardKnownList(this));
	}

	/**
	 * Return True if hte attacker is a L2MonsterInstance.<BR>
	 * <BR>
	 */
	@Override
	public boolean isAutoAttackable(L2Character attacker) {
		if (attacker instanceof FuzeMonsterInstance)
			return ((FuzeMonsterInstance) attacker).isAggressive();
		else if (attacker instanceof FuzePcInstance){
			FuzePcInstance pc=(FuzePcInstance) attacker;
			switch(getTemplate().race){
			case HUMAN:
				return pc.getRace()==Race.ELF||pc.getKarmaStat() == KarmaStat.EVIL;
			case ELF:
				return pc.getRace()==Race.HUMAN||pc.getKarmaStat() == KarmaStat.EVIL;
			case ASURA:
				return pc.getKarmaStat() == KarmaStat.EVIL;
			}
		}
		return false;
	}

	/**
	 * 如果守卫离出生点过远则返回出生点 Notify the L2GuardInstance to return to its home location (AI_INTENTION_MOVE_TO) and clear its _aggroList.<BR>
	 * <BR>
	 */
	public void returnHome() {
		if (!isInsideRadius(getSpawn().getLocx(), getSpawn().getLocy(), 160)) {
			clearAggroList();
			getAI().setIntention(CtrlIntention.AI_INTENTION_MOVE_TO, new L2CharPosition(getSpawn().getLocx(), getSpawn().getLocy()));
		}
	}

	/**
	 * Manage actions when a player click on the L2GuardInstance.<BR>
	 * <BR>
	 * 
	 * <B><U> Actions on first click on the L2GuardInstance (Select it)</U> :</B><BR>
	 * <BR>
	 * <li>Set the L2GuardInstance as target of the L2PcInstance player (if necessary)</li> <li>Send a Server->Client packet MyTargetSelected to the L2PcInstance player (display the select window)</li> <li>Set the L2PcInstance Intention to AI_INTENTION_IDLE</li> <li>Send a Server->Client packet ValidateLocation to correct the L2GuardInstance position and heading on the client</li><BR>
	 * <BR>
	 * 
	 * <B><U> Actions on second click on the L2GuardInstance (Attack it/Interact with it)</U> :</B><BR>
	 * <BR>
	 * <li>If L2PcInstance is in the _aggroList of the L2GuardInstance, set the L2PcInstance Intention to AI_INTENTION_ATTACK</li> <li>If L2PcInstance is NOT in the _aggroList of the L2GuardInstance, set the L2PcInstance Intention to AI_INTENTION_INTERACT (after a distance verification) and show message</li><BR>
	 * <BR>
	 * 
	 * <B><U> Example of use </U> :</B><BR>
	 * <BR>
	 * <li>Client packet : Action, AttackRequest</li><BR>
	 * <BR>
	 * 
	 * @param player
	 *            The L2PcInstance that start an action on the L2GuardInstance
	 * 
	 */
	@Override
	public void onAction(FuzePcInstance player) {
		if (!canTarget(player))
			return;

		// 检查玩家是否已经目标了此对象
		if (getObjectId() != player.getTargetId()) {
			if (_log.isDebugEnabled())
				_log.debug(player.getObjectId() + ": Targetted guard " + getObjectId());

			// Set the target of the L2PcInstance player
			player.setTarget(this);
		}

		// 检查玩家是否在对象的仇恨列表中
		if (containsTarget(player)) {
			if (_log.isDebugEnabled())
				_log.debug(player.getObjectId() + ": Attacked guard " + getObjectId());

			//如果在仇恨列表中发起对此玩家的攻击
			player.getAI().setIntention(CtrlIntention.AI_INTENTION_ATTACK, this);
		} else {
		    NpcTemplate npcTemplate = getTemplate();
			//-- 判断种族是否匹配
			if (!((npcTemplate.race == null) || (player.getRace() == npcTemplate.race) || npcTemplate.race == Race.ASURA || npcTemplate.race == Race.DEMON)) {
				return;
			}
			
			// 检查玩家与此NPC交互的条件是否满足
			if (canInteract(player)) {
				// Open a chat window on client with the text of the L2GuardInstance
				Quest[] qlsa = getTemplate().getEventQuests(Quest.QuestEventType.QUEST_START);
				if ((qlsa != null) && qlsa.length > 0)
					player.setLastQuestNpcObject(getObjectId());
				Quest[] qlst = getTemplate().getEventQuests(Quest.QuestEventType.ON_FIRST_TALK);
				if ((qlst != null) && qlst.length == 1)
					qlst[0].notifyFirstTalk(this, player);
				else
					showChatWindow(player, 0);
			}
		}
	}
}
